package org.geekbang.time.mathbasicthought.lesson8_combination;

import java.util.ArrayList;
import java.util.Arrays;

public class TeamRace {

    public static void main(String[] args) {
        ArrayList<String> teams = new ArrayList<>(Arrays.asList("t1", "t2", "t3"));
        combine(teams, new ArrayList<>(), 2);
    }

    /**
     * @param teams  还剩多少队伍没有参与组合
     * @param result 已经选出的队伍
     * @param m      选几只队伍
     */
    public static void combine(ArrayList<String> teams, ArrayList<String> result, int m) {
        if (result.size() == m) {
            System.out.println(result);
            return;
        }

        for (int i = 0; i < teams.size(); i++) {
            // 从剩下的队伍中，选出一队，加入结果
            ArrayList<String> newResult = (ArrayList<String>) result.clone();
            newResult.add(teams.get(i));

            // 只考虑当前选择之后的所有队伍
            ArrayList<String> restTeams = new ArrayList<>(teams.subList(i + 1, teams.size()));

            combine(restTeams, newResult, m);
        }
    }
}
